import pandas as pd
import joblib


def predic(personInfo):
    print(personInfo)
    Person = {
        'Sex': 1,
        'Pclass_1': 0,
        'Pclass_2': 0,
        'Pclass_3': 0,
        'Familysize': 0,
        'Familysize_Single': 0,
        'Familysize_Small': 0,
        'Familysize_Large': 0,
        'Child': 0,
        'Teenager': 0,
        'Youth': 0,
        'Middle_aged': 0,
        'Older': 0,
        'Master': 0,
        'Miss': 0,
        'Mr': 0,
        'Mrs': 0,
        'Officer': 0,
        'Royalty': 0,
        'Fare': 0,
        'Cabin_A': 0,
        'Cabin_B': 0,
        'Cabin_C': 0,
        'Cabin_D': 0,
        'Cabin_E': 0,
        'Cabin_F': 0,
        'Cabin_G': 0,
        'Cabin_H': 0,
        'Cabin_T': 0,
        'Embarked_C': 0,
        'Embarked_Q': 0,
        'Embarked_S': 0
    }
    if personInfo['Pclass'] == '1':
        Person['Pclass_1'] = 1
    elif personInfo['Pclass'] == "2":
        Person['Pclass_2'] = 1
    else:
        Person['Pclass_3'] = 1

    family = int(personInfo['SibSp'], 10) + int(personInfo['Parch'], 10) + 1
    if family <= 1:
        Person['Familysize_Single'] = 1
    elif 1 < family < 5:
        Person['Familysize_Small'] = 1
    else:
        Person['Familysize_Large'] = 1

    age = int(personInfo['Age'], 10)
    if age <= 6:
        Person['Child'] = 1
    elif 6 < age < 18:
        Person['Teenager'] = 1
    elif 18 < age < 40:
        Person['Youth'] = 1
    elif 40 < age < 60:
        Person['Middle_aged'] = 1
    else:
        Person['Older'] = 1

    modle = joblib.load('modle/titanic.modle')
    personDf = pd.DataFrame(Person, index=[0])
    res = modle.predict(personDf)
    bili = modle.predict_proba(personDf)
    result = {'res': res[0], 'die': bili[0][0], 'save': bili[0][1]}
    return result
